package com.example.administrator.q1;


import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class sayYes extends AppCompatActivity implements View.OnClickListener {

    // TODO: 获取fragment页面，设置监听事件，用fragmentmanager来管理。。。。
    Button button;

    FrameLayout frameLayout;

    Fragment fragmentOne;
    Fragment fragmentTwo;
    Fragment fragmentThree;
    Fragment fragmentFour;

    android.support.v4.app.FragmentManager fragmentManager;


    RelativeLayout relativeLayout_one;
    RelativeLayout relativeLayout_three;
    RelativeLayout relativeLayout_two;
    RelativeLayout relativeLayout_four;

    View view;
    TextView textView;
    ImageView titleLeftImageView;
    ImageView titleRightImageView;

    private long firstTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_say_yes);

        // button = (Button)findViewById(R.id.button_showList);
        view = (View) findViewById(R.id.say_yes_title);
        titleLeftImageView = (ImageView) view.findViewById(R.id.titleLeftImageView);
        titleRightImageView = (ImageView) view.findViewById(R.id.titleRightImageView);
        textView = (TextView) view.findViewById(R.id.titleTextView);

        titleLeftImageView.setImageResource(R.mipmap.ic_launcher_round);
        titleRightImageView.setImageResource(R.mipmap.ic_launcher_round);
        textView.setText(getResources().getText(R.string.Say_Yes_Title));
        textView.setText(R.string.Say_Yes_Title);

        relativeLayout_one = (RelativeLayout) findViewById(R.id.relativeLayout_one);
        relativeLayout_two = (RelativeLayout) findViewById(R.id.relativeLayout_two);
        relativeLayout_three = (RelativeLayout) findViewById(R.id.relativeLayout_three);
        relativeLayout_four = (RelativeLayout) findViewById(R.id.relativeLayout_four);

        relativeLayout_one.setOnClickListener(this);
        relativeLayout_two.setOnClickListener(this);
        relativeLayout_three.setOnClickListener(this);
        relativeLayout_four.setOnClickListener(this);

        titleLeftImageView.setOnClickListener(this);
        titleRightImageView.setOnClickListener(this);

        fragmentManager = getSupportFragmentManager();
        setChoice(4);
        // button.setOnClickListener(new View.OnClickListener() {
        //     @Override
        //     public void onClick(View view) {
        //         // Toast.makeText(sayYes.this, "成功了吗？", Toast.LENGTH_SHORT).show();
        //          Intent intent = new Intent();
        //          intent.setClass(sayYes.this, GridActivity.class);
        //          startActivity(intent);
        //          // finish();
        //     }
        // });
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            // 双击退出
            if (System.currentTimeMillis() - firstTime >= 1500) {
                Toast.makeText(sayYes.this, "再次点击退出", Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            // Toast.makeText(sayYes.this, "这是回退键", Toast.LENGTH_LONG).show();

            // 对话框试退出
            // AlertDialog.Builder alertDialog = new AlertDialog.Builder(sayYes.this);
            // alertDialog.setTitle("你确定要...")
            //         .setIcon(R.mipmap.ic_launcher)
            //         .setNegativeButton("取消？", new DialogInterface.OnClickListener() {
            //             @Override
            //             public void onClick(DialogInterface dialogInterface, int i) {
            //                 Toast.makeText(sayYes.this, "取消啦。。。？", Toast.LENGTH_LONG).show();

            //             }
            //         })
            //         .setPositiveButton("确定?", new DialogInterface.OnClickListener() {
            //             @Override
            //             public void onClick(DialogInterface dialogInterface, int i) {
            //                 Toast.makeText(sayYes.this, "关闭了。。。", Toast.LENGTH_SHORT).show();
            //                 finish();
            //             }
            //         })
            //         .setMessage("哈哈哈哈，成功了点点！");
            // ;
            // alertDialog.show();
        }
        return false;
    }

    @Override
    public void onClick(View view) {
        clear();
        switch (view.getId()) {
            case R.id.relativeLayout_one:
                setChoice(1);
                break;
            case R.id.relativeLayout_two:
                setChoice(2);
                break;
            case R.id.relativeLayout_three:
                setChoice(3);
                break;
            case R.id.relativeLayout_four:
                setChoice(4);
                break;
            case R.id.titleLeftImageView:
                // Toast.makeText(sayYes.this, "Left", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:17807710593"));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
            case R.id.titleRightImageView:
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(sayYes.this);
                alertDialog.setIcon(R.mipmap.ic_launcher_round)
                        .setTitle(R.string.Say_Yes_Title)
                        .setCancelable(true)
                        .setNegativeButton("Are you Ok ? Check it now!", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(sayYes.this, "Checked, Ok now!", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent();
                                intent.setAction(Intent.ACTION_DIAL);
                                intent.setData(Uri.parse("tel:17807710593"));
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                            }
                        })
                        .setPositiveButton("Ok, You are Checked. ", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(sayYes.this, "GoodMorning Sir, You ARE Checked.", Toast.LENGTH_SHORT).show();
                            }
                        });
                alertDialog.show();
                break;
            default:
                break;
        }
    }

    public void clear() {
        relativeLayout_one.setBackgroundColor(0xffffff);
        relativeLayout_two.setBackgroundColor(0xffffff);
        relativeLayout_three.setBackgroundColor(0xffffff);
        relativeLayout_four.setBackgroundColor(0xffffff);
    }

    public void setFragmentHide(FragmentTransaction fragmentTransaction) {
        if (fragmentOne != null) {
            fragmentTransaction.hide(fragmentOne);
        }
        if (fragmentThree != null) {
            fragmentTransaction.hide(fragmentThree);
        }
        if (fragmentTwo != null) {
            fragmentTransaction.hide(fragmentTwo);
        }
        if (fragmentFour != null) {
            fragmentTransaction.hide(fragmentFour);
        }
    }

    public void setChoice(int index) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        clear();
        setFragmentHide(fragmentTransaction);
        switch (index) {
            case 1:
                relativeLayout_one.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                if (fragmentOne == null) {
                    fragmentOne = new FragmentOne();
                    fragmentTransaction.add(R.id.FrameLayout_content, fragmentOne);
                } else {
                    fragmentTransaction.show(fragmentOne);
                }
                break;
            case 2:
                relativeLayout_two.setBackgroundColor(getResources().getColor(R.color.color_cyan));
                if (fragmentTwo == null) {
                    fragmentTwo = new FragmentTwo();
                    fragmentTransaction.add(R.id.FrameLayout_content, fragmentTwo);
                } else {
                    fragmentTransaction.show(fragmentTwo);
                }
                break;
            case 3:
                relativeLayout_three.setBackgroundColor(getResources().getColor(R.color.color_pink));
                if (fragmentThree == null) {
                    fragmentThree = new FragmentThree();
                    fragmentTransaction.add(R.id.FrameLayout_content, fragmentThree);
                } else {
                    fragmentTransaction.show(fragmentThree);
                }
                break;
            case 4:
                relativeLayout_four.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                if (fragmentFour == null) {
                    fragmentFour = new FragmentFour();
                    fragmentTransaction.add(R.id.FrameLayout_content, fragmentFour);
                } else {
                    fragmentTransaction.show(fragmentFour);
                }
                break;
        }
        fragmentTransaction.commit();
    }
}
